package com.six.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.six.core.entity.CheckShop;
import com.six.core.entity.Shop;
import com.six.core.util.JwtUtil;
import com.six.core.util.WebUtil;
import com.six.core.vo.Result;
import com.six.core.vo.ShopAndUserVo;
import com.six.shop.dao.CheckShopDao;
import com.six.shop.dao.ShopDao;
import com.six.shop.service.ShopService;
import com.six.shop.vo.SearchShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * ClassName:ShopServiceImpl
 * Description:
 * date:2023/2/27 10:11
 *
 * @since JDK 1.8
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopDao, Shop> implements ShopService {
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private CheckShopDao checkShopDao;
    @Override
    public Result findAll(SearchShop shop,Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Shop> list = shopDao.findAll(shop);
        PageInfo<Shop> pageInfo = new PageInfo<>(list);
        return new Result(2000,"查询成功",pageInfo);
    }

    @Override
    public Result findAllShop() {
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.decodeJWT(token);
        Integer tbuserid = (Integer) tokenChaim.get("tbUserid");
        List<Shop> allShop = shopDao.findAllShop(tbuserid.toString());
        return new Result(2000,"查询成功",allShop);
    }

    @Override
    public Result UpdateStateTrue(Integer shopid) {
        shopDao.updateStatetrue(shopid);
        return new Result(2000,"修改成功");
    }

    @Override
    public Result UpdateStateStart(Integer shopid) {
        shopDao.updateStateStart(shopid);
        return new Result(2000,"修改成功");
    }

    @Override
    public Result UpdateStateFales(Shop shop) {
        int i = shopDao.updateById(shop);
        return new Result(2000,"修改成功",i);
    }

    @Override
    public Integer getShopId(Integer userid) {
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userid);
        Shop shop = shopDao.selectOne(queryWrapper);
        return shop.getId();
    }

    @Override
    public Result insertShop(Shop shop) {
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.decodeJWT(token);
        Integer tbUserid = (Integer) tokenChaim.get("tbUserid");
        shop.setUserid(tbUserid);
        int insert = shopDao.insert(shop);
        //审核
        if (insert>0){
            CheckShop checkShop = new CheckShop();
            checkShop.setShopid(shop.getId());
            checkShop.setRemark(shop.getRemark());
            checkShop.setGmtCreate(new Date());
            checkShop.setState(0);
            checkShopDao.insert(checkShop);
        }
        return new Result(2000,"添加成功",insert);
    }

    @Override
    public List<ShopAndUserVo> findShopAndPrice() {
        List<ShopAndUserVo> shopAndPrice = shopDao.findShopAndPrice();
        return shopAndPrice;
    }

    @Override
    public Integer add(Shop shop) {
        return shopDao.insert(shop);
    }

    @Override
    public Integer findAllByNum() {
        List<Shop> list = shopDao.findAllNum();
        return list.size();
    }
}
